package main

import(
	"fmt"
	"strconv"
	 "golang.org/x/net/websocket"
)


type ClientSysHandler struct{
	flag int
	ids []int
	str string
}
type Client struct{
	uid int
	C chan string
	SysC chan ClientSysHandler
	name string
	addr string
	localAddr string
	conn *websocket.Conn
	room int
}


func (this *Client) start(){
	for{
		select{
		case msg:= <- this.C:
			this.CastSelf(msg)
		case SysHandler:= <- this.SysC:
			switch SysHandler.flag{
			case 0:
				id := SysHandler.ids[0]
				this.CastSelf("退出房间：" + strconv.Itoa(id))
			case 1:
				id := SysHandler.ids[0]
				this.room = id
				clientList[this.uid] = *this
				this.CastSelf("加入房间：" + strconv.Itoa(id))
			case 2:
				this.room = 0
				clientList[this.uid] = *this
				this.CastSelf("返回大厅")
			}
			
		}
	}
}



func (this *Client) CastSelf(msg string){
	if err := websocket.Message.Send(this.conn, msg); err != nil{
		fmt.Printf("uid= %d 发送消息错误 %v", this.uid, err.Error())
	}
}
func (this *Client) SysHandler(flag int, ids []int, msg string){
	this.SysC <- ClientSysHandler{flag:flag, ids: ids, str: msg}
}



